package vn.edu.uit.chemistry;

import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;

import vn.edu.uit.chemistry.utilities.TCPClient;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class GuiTaiLieu extends Activity {

	TextView tvTitle;
	
	TextView tvFile;
	TextView tvSize;
	TextView tvReceiver;
	
	TextView tvPercent;
	TextView tvXong;
	ProgressBar prPercent;
	
	ImageView imgGuiFile;
	
	Button btnBack;
	String path = "";
	String title = "";

	String tenGV = "";
	String ipGV = "";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_nop_bai_cu);

		tvTitle = (TextView) findViewById(R.id.tvTitle);
		
		Intent startIntent = getIntent();
		if (startIntent != null) {
			Bundle b = startIntent.getExtras();
			path = b.getString("File");
			title = b.getString("Title");
		}
		
		if (title.equals("TL")) {
			tvTitle.setText("Gửi tài liệu");
		} else if (title.equals("TLBK")) {
			tvTitle.setText("Gửi tài liệu tham khảo");
		}
		
		SharedPreferences prefs = getSharedPreferences("TaiKhoanGV", 0);
		tenGV = prefs.getString("hoten", "").toString();
		ipGV = prefs.getString("ip", "").toString();
		File f = new File(path);
		
		tvFile = (TextView) findViewById(R.id.tvFile);
		tvFile.setText(f.getName());
		
		tvSize = (TextView) findViewById(R.id.tvSize);
		tvSize.setText(""+f.length()+" bytes");
		
		tvReceiver = (TextView) findViewById(R.id.tvReceiver);
		tvReceiver.setText(tenGV);
		
		imgGuiFile = (ImageView) findViewById(R.id.imgGuiFile);
		imgGuiFile.postDelayed(onEverySecond, 0);
		
		tvPercent = (TextView) findViewById(R.id.tvPercent);
		prPercent = (ProgressBar) findViewById(R.id.prPercent);
		tvXong = (TextView) findViewById(R.id.tvXong);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		new SendAsyncTask().execute();
	}
	
	final class SendAsyncTask extends AsyncTask<Void, Void, Void> {
		
		public SendAsyncTask() {
			
		}

		@Override
		protected Void doInBackground(Void... param) {
			try {
				if (title.equals("TL")) {
					TCPClient client = new TCPClient(getApplicationContext(), ipGV, 54321);
					client.guiTaiLieuChoGV(path);
				} else if (title.equals("TLBK")) {
					TCPClient client = new TCPClient(getApplicationContext(), ipGV, 54321);
					client.guiFileBatKyChoGV(path);
				}
				
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}
		
	}
	
	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new UpdateSendAsyncTask().execute();
		}
	};
	
	final class UpdateSendAsyncTask extends AsyncTask<Void, Void, Void> {

		private int finish = 0;
		
		public UpdateSendAsyncTask() {
			
		}
		
		@Override
		protected Void doInBackground(Void... param) {
			SharedPreferences ref = getSharedPreferences("GuiFileGV", 0);
			finish = ref.getInt("finish", 0);
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			if (finish != 0) {
				Toast.makeText(getApplicationContext(), "Gửi tài liệu thành công.", Toast.LENGTH_LONG).show();
				imgGuiFile.removeCallbacks(onEverySecond);
				SharedPreferences ref = getSharedPreferences("GuiFileGV", 0);
				SharedPreferences.Editor editor = ref.edit();
				editor.putInt("finish", 0);
				editor.commit();
				tvPercent.setVisibility(View.GONE);
				prPercent.setVisibility(View.GONE);
				tvXong.setVisibility(View.VISIBLE);
				//finish();
			} else {
				imgGuiFile.postDelayed(onEverySecond, 0);
			}
		}
		
	}
}
